package com.lkhhd.volunteer.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lkhd.common.myemum.CategoryEnum;
import com.lkhd.common.myemum.VolunteerEnum;
import com.lkhd.common.utils.IDUtil;
import com.lkhd.manage.mapper.JobMapper;
import com.lkhd.manage.mapper.UserMapper;
import com.lkhd.manage.mapper.VolunteerMapper;
import com.lkhd.manage.pojo.Job;
import com.lkhd.manage.pojo.User;
import com.lkhd.manage.pojo.Volunteer;
import com.lkhd.manage.povo.VolunteerVo;
import com.lkhd.volunteer.service.VolunteerService;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class VolunteerServiceImpl implements VolunteerService {

	@Autowired
	private VolunteerMapper volunteerMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private JobMapper jobMapper;
	
	/**
	 * 	申请志愿者
	 * @param volunteer
	 * @return
	 */
	@Override
	public int applyFor(Volunteer volunteer) {
		volunteer.setVolunteerid(IDUtil.getId());
		volunteer.setActive(CategoryEnum.UP.getCode());
		volunteer.setActivitynum(0);
		volunteer.setGradenum(0);
		volunteer.setStatus(VolunteerEnum.CHECK.getCode());
		
		return volunteerMapper.insert(volunteer);
	}

	/**
	 * 	志愿者审核是否通过
	 * @param volunteerId
	 * @param isPass true 通过设置状态， false不通过直接删除
	 * @return
	 */
	@Override
	public int isPass(long volunteerId, boolean isPass) {
		if(isPass) {
			Volunteer volunteer = new Volunteer();
			volunteer.setVolunteerid(volunteerId);
			volunteer.setStatus(VolunteerEnum.PASS.getCode());
			return volunteerMapper.updateByPrimaryKeySelective(volunteer);
		}
		return volunteerMapper.deleteByPrimaryKey(volunteerId);
	}

	/**
	 * 	更新志愿者职位(管理员操作)
	 * @param volunteer
	 * @return
	 */
	@Override
	public int updateVolunteerJob(Volunteer volunteer) {
		return volunteerMapper.updateByPrimaryKeySelective(volunteer);
	}

	/**
	 *	 冻结/解冻志愿者(管理员操作)
	 * @param volunteer 设置状态 status -1冻结  0正在审核 1通过 
	 * 		管理员应该要设置为什么冻结理由
	 * 		以及status的状态-1
	 * @return
	 */
	@Override
	public int freeze(Volunteer volunteer) {
		// 管理员应该要设置为什么冻结理由
		// 以及status的状态
		return volunteerMapper.updateByPrimaryKeySelective(volunteer);
	}

	/**
	 *  	根据条件查询志愿者列表
	 * @param example
	 * @param pageNum
	 * @param pageSzie
	 * @return
	 */
	@Override
	public PageInfo<VolunteerVo> selectAllByVolunteer(Example example, int pageNum, int pageSzie) {
		PageHelper.startPage(pageNum, pageSzie);
		List<Volunteer> volunteers = volunteerMapper.selectByExample(example);
		
		PageInfo<Volunteer> pageInfo1 = new PageInfo<>(volunteers);
		PageInfo<VolunteerVo> pageInfo2 = new PageInfo<>();
		BeanUtils.copyProperties(pageInfo1, pageInfo2);
		
		// 封装带有用户信息的志愿者列表
		List<VolunteerVo>  volunteerVo = new ArrayList<>();
		for(Volunteer volunteer : volunteers) {
			VolunteerVo tempVo = new VolunteerVo();
			BeanUtils.copyProperties(volunteer, tempVo);
			User user = userMapper.selectByPrimaryKey(tempVo.getVolunteerid());
			user.setPassword(null);
			
			tempVo.setUser(user);
			volunteerVo.add(tempVo);
		}
		pageInfo2.setList(volunteerVo);
		return pageInfo2;
	}

	/**
	 * 	添加职位
	 * @param job
	 * @return
	 */
	@Override
	public int addJob(Job job) {
		job.setJobid(IDUtil.getId());
		return jobMapper.insert(job);
	}

	/**
	 * 	更新职位
	 * @param job
	 * @return
	 */
	@Override
	public int updateJob(Job job) {
		 return jobMapper.updateByPrimaryKeySelective(job);
	}

	/**
	 * 	查询所有职位
	 * @param example
	 * @return
	 */
	@Override
	public List<Job> selectAllJob(Example example) {
		return jobMapper.selectByExample(example);
	}

}
